package com.dynamic.mybatis.core.generator.helper;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dynamic.mybatis.core.generator.InjectorHelper;
import com.dynamic.mybatis.core.metadata.MappedStatementTable;

/**
 * @Description
 * @Author xs
 * @Date 2023/4/14 16:37
 */
public class OrderByInjectorHelper extends InjectorHelper {


    public static String orderByDefaultKeyColumn(MappedStatementTable table) {
        String keyColumn = getFirstKeyColumn(table);
        if (StringUtils.isBlank(keyColumn)) {
            return StringPool.EMPTY;
        }
        StringBuilder sql = new StringBuilder();
        sql.append(" ORDER BY %s %s");
        return String.format(sql.toString(), keyColumn, DESC);
        /* 当wrapper中传递了orderBy属性，@orderBy注解失效 */
        //    return SqlScriptUtils.convertIf(sql.toString(), String.format("%s == null or %s", WRAPPER,
        //           WRAPPER_EXPRESSION_ORDER), true);
    }
}